
import { _decorator, Component, Node, EventTouch } from 'cc';
import { EventConst } from '../../const/Const';
import eventUtil from '../modules/eventUtil';
const { ccclass, property } = _decorator;

@ccclass('TouchMgr')
export class TouchMgr extends Component {

    start() {
        this.addListener();

    }

    addListener() {

        this.node.on(Node.EventType.TOUCH_START, this.mouseDown, this)
        this.node.on(Node.EventType.TOUCH_MOVE, this.mouseMove, this)
        this.node.on(Node.EventType.TOUCH_CANCEL, this.mouseUp, this)
        this.node.on(Node.EventType.TOUCH_END, this.mouseUp, this)
    }

    mouseDown(e: EventTouch) {
        // Debug.log('mouseDown ')
        // if (!Lite.myGame.gameStarted)
        //     return;
        // Debug.log('donw e.target.name', e.target.name)
        // if (this.mListen.indexOf(e.target.name) == -1) return;
        // let touchPoint = e.getLocationInView();
        eventUtil.sendEventImmediately(EventConst.TOUCH_DOWN, e)
    }
    mouseMove(e: EventTouch) {
        // if (!Lite.myGame.gameStarted)
        //     return;

        // Debug.log('mouseMove')
        // if (this.mListen.indexOf(e.target.name) == -1) return;
        eventUtil.sendEventImmediately(EventConst.TOUCH_MOVE, e)
    }
    mouseUp(e: EventTouch) {
        // if (!Lite.myGame.gameStarted)
        //     return;
        // Debug.log('mouseUp')
        // if (this.mListen.indexOf(e.target.name) == -1) return;
        eventUtil.sendEventImmediately(EventConst.TOUCH_UP, e)
    }
    removeListener() {
        this.node.off(Node.EventType.TOUCH_START, this.mouseDown, this)
        this.node.off(Node.EventType.TOUCH_MOVE, this.mouseMove, this)
        this.node.off(Node.EventType.TOUCH_CANCEL, this.mouseUp, this)
    }

    onDisable() {
        this.removeListener();
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
